package com.sskj.gocoin.v.activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.mine.FeedBackActivity;
import com.sskj.gocoin.p.activity.AboutUsPresenter;
import com.sskj.gocoin.utils.APKVersionCodeUtils;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.CopyUtil;
import com.sskj.gocoin.utils.IntentUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;
import me.jessyan.autosize.utils.ScreenUtils;

/**
 * <pre>
 *     author : 吕志豪
 *     e-mail : 1030753080@qq.com
 *     time   : 2018/04/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class AboutUsActivity extends BaseActivity<AboutUsPresenter> {
    @BindView(R2.id.contract_us)
    LinearLayout rlContactUs;
    @BindView(R2.id.llVip)
    LinearLayout llVip;
    @BindView(R2.id.llRate)
    LinearLayout llRate;
    @BindView(R2.id.feed_back)
    LinearLayout feedBack;
    @BindView(R2.id.toolBar)
    ToolBarLayout toolBar;
    @BindView(R2.id.rules)
    TextView rules;
    @BindView(R2.id.service)
    TextView service;
    @BindView(R2.id.version)
    TextView version;
    @BindView(R2.id.facebook)
    ImageView facebook;
    @BindView(R2.id.twitter)
    ImageView twitter;
    @BindView(R2.id.github)
    ImageView github;
    @BindView(R2.id.tvContent)
    TextView tvContent;

    private String email;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_about_us;
    }

    @Override
    public AboutUsPresenter getPresenter() {
        return new AboutUsPresenter();
    }

    @Override
    protected void onViewBinding() {
        mPresenter.getEmail();
        toolBar.setLeftButtonOnClickLinster(v -> finish());
        ClickUtil.click(rlContactUs, () -> {
            View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_connect, null);
            AlertDialog dialog = new AlertDialog.Builder(this, R.style.custom_dialog)
                    .setView(dialogView)
                    .show();
            Window window = dialog.getWindow();
            int[] size = ScreenUtils.getScreenSize(this);
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = (int) (size[0] * 0.9f);
            window.setAttributes(layoutParams);
            TextView textView = dialogView.findViewById(R.id.content);
            TextView close = dialogView.findViewById(R.id.close);
            TextView tvCopy = dialogView.findViewById(R.id.tvCopy);
            setText(textView, email);
            Button button = dialogView.findViewById(R.id.submit);
            button.setOnClickListener(v -> dialog.dismiss());
            close.setOnClickListener(v -> dialog.dismiss());
            tvCopy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!TextUtils.isEmpty(email)) {
                        CopyUtil.copy(email);
                        dialog.dismiss();
                    }
                }
            });
        });
        ClickUtil.click(feedBack, () -> {
            FeedBackActivity.start(this);
        });
        ClickUtil.click(llVip, () -> {

            Bundle bundle = new Bundle();
            bundle.putString(Constans.INTENT_NOTICE_ID, "vip");
            ARouter.getInstance().build(RConfig.MINE_WEBVIEW).with(bundle).navigation();
        });
        ClickUtil.click(llRate, () -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constans.INTENT_NOTICE_ID, "jygz");
            ARouter.getInstance().build(RConfig.MINE_WEBVIEW).with(bundle).navigation();
        });
        mPresenter.getContent();

    }

    /**
     * 自定义布局
     * setView()只会覆盖AlertDialog的Title与Button之间的那部分，而setContentView()则会覆盖全部，
     * setContentView()必须放在show()的后面
     */
    public void setEmail(String emailStr) {
        email = emailStr;
    }

    public void setContent(String data) {
        RichText.fromHtml(formatImg(data)).bind(this).into(tvContent);
    }

    @Override
    protected void initView() {
        initFooter();
    }

    public String formatImg(String content) {
        Document document = Jsoup.parse(content);
        Elements imgs = document.getElementsByTag("img");
        for (int i = 0; i < imgs.size(); i++) {
            Element img = imgs.get(i);
            img.attr("width", "100%").attr("height", "auto");
            String url = img.attr("src");
            if (!url.contains("http")) {
                url = HttpConfig.BASE_URL + url;
            }
            img.attr("src", url);
        }
        return document.toString();
    }


    public void initFooter() {
        setText(version, App.INSTANCE.getString(R.string.fabimineJavaAboutUsActivity9) + APKVersionCodeUtils.getVerName(this));
        ClickUtil.click(rules, () -> WebViewActivity.start(this, "trans_agree"));
        ClickUtil.click(service, () -> WebViewActivity.start(this, "reg_agree"));
        ClickUtil.click(facebook, () -> IntentUtil.browser(this, "https://www.facebook.com/GoCoiner"));
        ClickUtil.click(github, () -> IntentUtil.browser(this, "https://github.com/GoCoin"));
        ClickUtil.click(twitter, () -> IntentUtil.browser(this, "https://gocoin.com/twitter"));
    }
}
